package com.iapplize.gcm.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.android.gcm.GCMRegistrar;
import com.iapplize.gcm.test.SendMessageDialog.SendMessageDialogListener;
import com.iapplize.http.HttpConnect;
import com.iapplize.objects.GlobalVariables;
import com.iapplize.objects.User;
import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class AllUsersActivity extends FragmentActivity implements OnItemClickListener, SendMessageDialogListener{

	
	public MyApplication appState;
	
	public ProgressDialog spinnerDialog;
	
	List<User> user = new ArrayList<User>();
	
	PullToRefreshListView lv;
	
	private int selectedIndex = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_all_users);

		appState = ((MyApplication) getApplicationContext());
		appState.setMainActivety(this);
		
		lv = (PullToRefreshListView) findViewById(android.R.id.list);
		lv.setOnItemClickListener(this);
		
		lv.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				// Do work to refresh the list here.

				
				getData();
				
			}

		});
		
		
		getData();
		
	}
	
	public void getData(){
		spinnerDialog = ProgressDialog.show(this, "",
				getString(R.string.Connecting_to_server), true);

		HttpConnect hc1 = new HttpConnect(AllUsersActivity.this,
				HttpConnect.GCM_GET_USER_LIST);
		hc1.execute("");
	}
	
	public void updateList(List<User> user){
		
		this.user = user;
		
		for (int i = 0; i < user.size(); i++) {
			if(user.get(i).MacAddress.equalsIgnoreCase(Installation.id(this).replace("-", ""))){
				user.remove(i);
			}
		}
		
		
		List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

		Map<String, Object> data;
		
		for (int i = 0; i < user.size(); i++) {
			data = new HashMap<String, Object>();

			data.put("text1", user.get(i).Email);

			resourceNames.add(data);
		}
		
		SimpleAdapter notes = new SimpleAdapter(this, resourceNames,
				android.R.layout.simple_list_item_1, new String[] { "text1"},
				new int[] { android.R.id.text1});

		lv.setAdapter(notes);
		
		lv.onRefreshComplete();
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		appState = ((MyApplication) getApplicationContext());
		appState.setMainActivety(this);
	}
	
	private void showSendMessageDialog() {
        FragmentManager fm = getSupportFragmentManager();
        SendMessageDialog sendMessageDialog = new SendMessageDialog();
        sendMessageDialog.show(fm, getString(R.string.Write_a_message));
    }
	
	public void onFinishEditDialog(String inputText) {
	       
		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {
			GCMRegistrar.register(this, GlobalVariables.getSenderId());
			Toast.makeText(this, R.string.Try_again_later,
					Toast.LENGTH_SHORT).show();
		} else {
			Log.v("GCM - test", "Already registered");

			if (appState.checkConnecion()) {
				
				spinnerDialog = ProgressDialog.show(this, "",
						getString(R.string.Connecting_to_server), true);

				HttpConnect hc1 = new HttpConnect(AllUsersActivity.this,
						HttpConnect.GCM_SEND_MESSAGE);
				
				String [] mes = new String[2];
				mes[0] = user.get(selectedIndex-1).RegId;
				mes[1] = inputText.toString();
				
				hc1.execute(mes);
			}else{
				Toast.makeText(this, R.string.No_Internet_connection,
						Toast.LENGTH_SHORT).show();
			}
		}
		
		
		
    }

	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		// TODO Auto-generated method stub
		selectedIndex = pos;
		
		showSendMessageDialog();
	}
}
